-- TrainingM
-- Create by zouyb
-- 训练室模块

module("TrainingM", package.seeall);

-- 训练信息
local training_info = {};

-- 升阶状态
TRAINING_STATE_PREPARE    = 1;
TRAINING_STATE_UPGRADING  = 2;
TRAINING_STATE_FINISH     = 3;

-- 模块初始化
function init()
    training_info = {};
end

-- 更新训练信息
function updateInfo(info)
    training_info = info;

    EventMgr.fire(event.TRAINING_INFO_UPDATED);
end

-- 获取训练信息
function getInfo()
    return training_info;
end

-- 获取升阶位上的英雄ID
function getHeroId()
    if training_info == nil or training_info.cur_hero == nil then
        return 0;
    end

    return training_info.cur_hero;
end

-- 获取升阶完成时间
function getFinishTime()
    if training_info == nil or training_info.finish_time == nil then
        return -1;
    end

    return training_info.finish_time;
end

-- 获取训练室等级
function getLevel()
    if training_info == nil or training_info.level == nil then
        return 0;
    end

    return training_info.level;
end

-- 设置训练室等级
function setLevel(level)
    if training_info == nil or training_info.level == nil then
        return;
    end

    training_info.level = level;
end

-- 设置进阶结束
function setUpgradeEnd()
    training_info.cur_hero = 0;
    training_info.finish_time = nil;
end

-- 是否正在进阶
function isUpgrading(classId)
    if getHeroId() == classId and getState() ~= TRAINING_STATE_PREPARE then
        return true;
    end

    return false;
end

-- 开始升阶
function starUpgrade(classId)
    local pet = PetM.getMyPet(classId);
    local costTime = FormulaM.invoke("CALC_PET_UPGRADE_COST_TIME", pet);
    -- 服务器当前时间
    local serverTime = TimeM.getServerTime();
    training_info.cur_hero = classId;
    training_info.finish_time = serverTime + costTime;

    EventMgr.fire(event.TRAINING_INFO_UPDATED);
end

-- 获取当前英雄升阶状态
function getState()
    -- 获取完成时间
    local finishTime = getFinishTime();
    -- 服务器当前时间
    local serverTime = TimeM.getServerTime();

    if finishTime < 0 then
        return TRAINING_STATE_PREPARE;
    elseif finishTime == 0 or serverTime >= finishTime then
        return TRAINING_STATE_FINISH;
    else
        return TRAINING_STATE_UPGRADING;
    end
end

-- 是否可以升阶
function canUpgrade(classId)
    local pet = PetM.getMyPet(classId);
    if PetM.isActivePet(classId) then
        return "your_pet_is_active";
    end

    local lictors = LictorM.getLictorPets();
    for _, c in pairs(lictors) do
        -- 必须跟出战宠物同阵营
        if c == classId and DungeonM.isInDungeon() and DungeonM.isPause() then
            return "your_pet_is_lictor";
        end
    end

    local starNum = pet:getRank();
    local maxStarNum = 5;
    if maxStarNum <= starNum then
        return "reach_max_star_num";
    end

    local level = getLevel();
    if pet:getRank() >= level + 1 then
        return "hero_upgrade_hint";
    end

    local weaponClassId = EquipM.getHeroEquip(pet, EQUIP_TYPE_WEAPON, 1);
    local medalClassId = EquipM.getHeroEquip(pet, EQUIP_TYPE_MEDAL, 1);
    if weaponClassId == nil then
        return "lack_of_weapon";
    end

    if medalClassId == nil then
        return "lack_of_medal";
    end

    return true;
end

-- 是否有英雄可升阶
function canHeroUpgrade()
    local heroList = ME.user:getAllPetIds();
    for _, id in pairs(heroList) do
        if canUpgrade(id) == true then
            return true;
        end
    end

    return false;
end


